//
//  ShowtimeDataModel.m
//  ShowTimeApp
//
//  Created by Ruby on 11/6/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ShowtimeDataModel.h"
#import "CinemaService.h"
#import "CinemaDataModel.h"
#import "MovieDataModel.h"

@implementation ShowtimeDataModel

-(id)initShowtimeDataModel{
    [self initBaseDataModel];
    //[self addObjects];
    return self;
}

-(ShowTime*)getDetailByID:(int)ID{
    ShowTime* st = nil;
    NSString* condition=[NSString stringWithFormat:@"ID==%@",[NSNumber numberWithInt:ID]];
    NSPredicate* predicate = [NSPredicate predicateWithFormat:condition];
    st= (ShowTime*)[self fetchObject:@"ShowTime" byPredicate:predicate];
    return st;
}

-(void)insertObject:(id)showtime{
    CinemaService_ShowTimeEntity *newShowtime =(CinemaService_ShowTimeEntity*)showtime;
    ShowTime *st = (ShowTime*)[NSEntityDescription insertNewObjectForEntityForName:@"ShowTime" inManagedObjectContext:self.managedObjectContext];
    st.ID = [newShowtime ID_];
    st.MovieID = [[newShowtime MovieFK]ID_];
    st.CinemaID= [[newShowtime CinemaFK] ID_];
    st.Date=[newShowtime Date];
    st.Time=[newShowtime Time];
    st.Movie=[MovieDataModel convertFromCinemaService_MovieEntity:[newShowtime MovieFK] TodbMovieEntity:st.Movie];
    st.Cinema=[CinemaDataModel convertFromCinemaService_CinemaEntity:[newShowtime CinemaFK] TodbCinemaEntity:st.Cinema];
    
//    st.Movie.ID = [[newShowtime MovieFK]ID_];
//    st.Movie.TitleEN=[[newShowtime MovieFK] TitleEN];
//    st.Movie.TitleVI=[[newShowtime MovieFK] TitleVI];
//    st.Movie.Genre = [[newShowtime MovieFK] Genre];
//    st.Movie.Description=[[newShowtime MovieFK] Description];
//    st.Movie.MovieStudio=[[newShowtime MovieFK] MovieStudio];
//    st.Movie.Director=[[newShowtime MovieFK] Director];
//    st.Movie.Writer=[[newShowtime MovieFK] Writer];
//    st.Movie.Actors=[[newShowtime MovieFK] Actors];
//    st.Movie.Language=[[newShowtime MovieFK] Language];
//    st.Movie.Length=[[newShowtime MovieFK] Length];
//    st.Movie.Version=[[newShowtime MovieFK] Version];
//    st.Movie.Country=[[newShowtime MovieFK] Country];
//    st.Movie.ImageUrl=[[newShowtime MovieFK] ImageUrl];
//    st.Movie.TrailerUrl=[[newShowtime MovieFK] TrailerUrl];
//    st.Movie.Rating=[[newShowtime MovieFK] Rating];
//    st.Movie.Discount=[[newShowtime MovieFK] Discount];
//    
//    st.Cinema.ID =[[newShowtime CinemaFK]ID_];
//    st.Cinema.Name = [[newShowtime CinemaFK] Name];
//    st.Cinema.Tel=[[newShowtime CinemaFK] Tel];
//    st.Cinema.Address=[[newShowtime CinemaFK] Address];
//    st.Cinema.Website = [[newShowtime CinemaFK] Website];
//    st.Cinema.ImageUrl = [[newShowtime CinemaFK] ImageUrl];
//    st.Cinema.GroupName=[[newShowtime CinemaFK] GroupName];
//    st.Cinema.Description=[[newShowtime CinemaFK] Description];
//    st.Cinema.City=[[newShowtime CinemaFK] City];
//    st.Cinema.Fax=[[newShowtime CinemaFK] Fax];
//    st.Cinema.Email=[[newShowtime CinemaFK] Email];

}

-(void)insertObjects:(NSMutableArray *)showtimeList{
    for (id s in showtimeList){
        [self insertObject:s];
    }
}
//-(void)insertObject:(id)showtime withCinemaList:(NSMutableArray*)cinemaList withMovieList:(NSMutableArray*)movieList{
//    CinemaService_ShowTimeEntity *newShowtime =(CinemaService_ShowTimeEntity*)showtime;
//    ShowTime *st = (ShowTime*)[NSEntityDescription insertNewObjectForEntityForName:@"ShowTime" inManagedObjectContext:self.managedObjectContext];
//    st.ID = [newShowtime ID_];
//    st.MovieID = [[newShowtime MovieFK]ID_];
//    st.CinemaID= [[newShowtime CinemaFK] ID_];
//    st.Date=[newShowtime Date];
//    st.Time=[newShowtime Time];
//    
//    for(id movie in movieList){
//        CinemaService_MovieEntity* m = (CinemaService_MovieEntity*)movie;
//
//        }
//    }
//    
//    for(id cinema in cinemaList){
//        Cinema* c = (Cinema*)cinema;
//        if(c.ID==[[newShowtime CinemaFK]ID_]){
//            st.Cinema=c;
//        }
//    }
//}

//-(void)insertObjects:(NSMutableArray *)showtimeList withCinemaList:(NSMutableArray*)cinemaList withMovieList:(NSMutableArray*)movieList{
//    for (id s in showtimeList){
//        [self insertObject:s withCinemaList:cinemaList withMovieList:movieList];
//    }
//}

-(void)deleteAll{
    // Add a sort descriptor
	NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Date" ascending:YES selector:nil];
    [self fetchObjects:@"ShowTime" sortBy:sortDescriptor];
    [self removeObjects];
}

-(NSMutableArray*)getAllShowtimeByCinemaSortByDateAscending:(int)cinemaID withMovieID:(int)movieID{
    // Add a sort descriptor
	NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Date" ascending:YES selector:nil];
    
    // Add a predicate
    NSString* condition= [NSString stringWithFormat:@"(CinemaID==%@) && (MovieID==%@)",[NSNumber numberWithInt:cinemaID],[NSNumber numberWithInt:movieID]];
    NSPredicate* predicate = [NSPredicate predicateWithFormat:condition];
    
    // Goi ham o lop cha
    [self fetchObjects:@"ShowTime" sortBy:sortDescriptor byPredicate:predicate];
    return (NSMutableArray*)fetchResultController.fetchedObjects;   
}

-(BOOL)isMovieInShowtimeOfCinema:(int)cinemaID withMovieID:(int)movieID{
    // Add a sort descriptor
	NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Date" ascending:YES selector:nil];
    
    // Add a predicate
    NSString* condition= [NSString stringWithFormat:@"(CinemaID==%@) && (MovieID==%@)",[NSNumber numberWithInt:cinemaID],[NSNumber numberWithInt:movieID]];
    NSPredicate* predicate = [NSPredicate predicateWithFormat:condition];
    
    // Goi ham o lop cha
    [self fetchObjects:@"ShowTime" sortBy:sortDescriptor byPredicate:predicate];
    //[self output:(NSMutableArray*)self.fetchResultController.fetchedObjects];
    if(self.fetchResultController.fetchedObjects.count!=0)
        return YES;
    else
        return NO;
}

-(void)output:(NSMutableArray *)showtimeList{
    if(showtimeList.count>0)
    {
        for (id st in showtimeList){ 
            ShowTime* s = (ShowTime*)st;
            NSLog(@"%@ - %@ - %@ - %@ - %@ - %@",s.CinemaID,s.MovieID,s.Date,s.Time,s.Cinema.Name,s.Movie.TitleEN);
        }
    }    
}
@end
